{{- $currentNode := . }}
{{- $showvisitedlinks := .Site.Params.showVisitedLinks -}}

{{- if eq .Site.Params.ordersectionsby "title"}}
  {{- range .Site.Sections.ByTitle}}
    {{- template "section-tree-nav" dict "sect" . "currentnode" $currentNode "showvisitedlinks" $showvisitedlinks}}
  {{- end}}
{{- else}}
  {{- if .Site.Home.Sections}}
    {{- .Scratch.Set "pages" (.Site.Home.RegularPages | union .Site.Home.Sections) }}
  {{- else -}}
    {{- .Scratch.Set "pages" .Site.Home.RegularPages }}
  {{- end}}
  {{- $pages := (.Scratch.Get "pages") }}
  {{- range $pages.ByWeight}}
    {{- template "section-tree-nav" dict "sect" . "currentnode" $currentNode "showvisitedlinks" $showvisitedlinks}}
  {{- end}}
{{- end}}

<!-- templates -->
{{- define "section-tree-nav" }}
{{- $showvisitedlinks := .showvisitedlinks }}
{{- $currentNode := .currentnode }}
 {{- with .sect}}
  {{- if and .IsSection (or (not .Params.hidden) $.showhidden)}}
    {{- $numberOfPages := (add (len .Pages) (len .Sections)) }}
    {{- safeHTML .Params.head}}
    <li data-nav-id="{{.Permalink}}" class="dd-item
        {{- if .IsAncestor $currentNode}} parent{{end}}
        {{- if eq .Permalink $currentNode.Permalink}} active{{end}}
        {{- if .Params.alwaysopen}} alwaysopen{{end -}}
        {{- if ne $numberOfPages 0 }} haschildren{{end}}
        ">
      <a href="{{ .RelPermalink}}">{{safeHTML .Params.Pre}}{{.Title}}{{safeHTML .Params.Post}}</a>

        {{- if ne $numberOfPages 0 }}
          {{- if or (.IsAncestor $currentNode) (.Params.alwaysopen) }}
            <i class="fa fa-angle-down fa-lg category-icon"></i>
          {{- else -}}
            <i class="fa fa-angle-right fa-lg category-icon"></i>
          {{- end}}
        {{- end}}

        {{- if $showvisitedlinks}}<i class="fa fa-circle-thin read-icon"></i>{{end}}
      {{- if ne $numberOfPages 0 }}
        <ul>
          {{- .Scratch.Set "pages" .Pages }}
          {{- if .Sections}}
          {{- .Scratch.Set "pages" (.Pages | union .Sections) }}
          {{- end}}
          {{- $pages := (.Scratch.Get "pages") }}

        {{- if eq .Site.Params.ordersectionsby "title"}}
          {{- range $pages.ByTitle }}
            {{- if and .Params.hidden (not $.showhidden) }}
            {{- else}}
            {{- template "section-tree-nav" dict "sect" . "currentnode" $currentNode "showvisitedlinks" $showvisitedlinks }}
            {{- end}}
          {{- end}}
        {{- else}}
          {{- range $pages.ByWeight }}
            {{- if and .Params.hidden (not $.showhidden) }}
            {{- else}}
            {{- template "section-tree-nav" dict "sect" . "currentnode" $currentNode "showvisitedlinks" $showvisitedlinks }}
            {{- end}}
          {{- end}}
        {{- end}}
        </ul>
      {{- end}}
    </li>
  {{- else}}
    {{- if not .Params.Hidden }}
      <li data-nav-id="{{.Permalink}}" class="dd-item
     {{- if eq .Permalink $currentNode.Permalink}} active{{end -}}
      ">
        <a href="{{ .RelPermalink}}">
          {{safeHTML .Params.Pre}}{{.LinkTitle}}{{safeHTML .Params.Post}}
        </a>
        {{- if $showvisitedlinks}}<i class="fa fa-circle-thin read-icon"></i>{{end}}
    </li>
     {{- end}}
  {{- end}}
 {{- end}}
{{- end}}
